package com.me.blood.frag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.me.blood.R;
import com.me.blood.databinding.DlgCalendarBinding;
import com.me.utils.Utils;
import com.roomorama.caldroid.CaldroidFragment;
import com.roomorama.caldroid.CaldroidListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CalendarDlg extends FullScreenDlg {

    DlgCalendarBinding binding;
    CaldroidFragment fragment;
    int mMonth, mYear;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DlgCalendarBinding.inflate(inflater, container, false);
        binding.container.setRatio(1);
        binding.close.setOnClickListener(v -> dismiss());
        binding.getRoot().setOnClickListener(v -> dismiss());

        Calendar c = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
        formatter.setTimeZone(TimeZone.getDefault()); //设置时区
        String createDate = formatter.format(c.getTime());   //格式转换
        binding.time.setText(createDate);
        binding.rightM.setOnClickListener(v -> {
            if (++mMonth > 12) {
                ++mYear;
                mMonth = 1;
            }
            updateDate();
        });
        binding.leftM.setOnClickListener(v -> {
            if (--mMonth <= 0) {
                --mYear;
                mMonth = 12;
            }
            updateDate();
        });
        binding.leftY.setOnClickListener(v -> {
            --mYear;
            updateDate();
        });
        binding.rightY.setOnClickListener(v -> {
            ++mYear;
            updateDate();
        });
        int h = Utils.getScreenWidth() - Utils.dp2Px(getActivity(), 28);
        fragment = buildCalendar(h);
        replaceFragment(R.id.container, fragment);

        return binding.getRoot();
    }

    private void updateDate() {
        Date d = new Date(mYear - 1900, mMonth - 1, 1);
        fragment.moveToDate(d);
    }

    private CaldroidFragment buildCalendar(int height) {
        // Setup caldroid fragment
        // **** If you want normal CaldroidFragment, use below line ****
        CaldroidFragment caldroidFragment = new CaldroidFragment();

        // //////////////////////////////////////////////////////////////////////
        // **** This is to show customized fragment. If you want customized
        // version, uncomment below line ****
//		 caldroidFragment = new CaldroidSampleCustomFragment();

        // Setup arguments
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();

        mYear = cal.get(Calendar.YEAR);
        mMonth = cal.get(Calendar.MONTH) + 1;

        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SQUARE_TEXT_VIEW_CELL, true);
        args.putBoolean(CaldroidFragment.SHOW_TITLE_VIEW, false);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, false);
        args.putInt(CaldroidFragment.HEIGHT, height);

        // Uncomment this to customize startDayOfWeek
        // args.putInt(CaldroidFragment.START_DAY_OF_WEEK,
        // CaldroidFragment.TUESDAY); // Tuesday

        // Uncomment this line to use Caldroid in compact mode
        // args.putBoolean(CaldroidFragment.SQUARE_TEXT_VIEW_CELL, false);

        // Uncomment this line to use dark theme
        args.putInt(CaldroidFragment.THEME_RESOURCE, R.style.MyCaldroid);
        caldroidFragment.setArguments(args);
        caldroidFragment.setCaldroidListener(new CaldroidListener() {
            @Override
            public void onChangeMonth(int month, int year) {
                mYear = year;
                mMonth = month;
                binding.time.setText(year + "年" + month + "月");
            }

            @Override
            public void onSelectDate(Date date, View view) {
                //    caldroidFragment.setSelectedDay(date);
                dismiss(true, date);
            }
        });

        return caldroidFragment;
    }
}
